

import os
import pickle


ONTOLOGIES_YES = document.getElementById("ontologies-yes")
ONTOLOGIES_NO = document.getElementById("ontologies-no")
LBL_SELECTED_ONTO = document.getElementById("selected_ontology")
ONTO_URL = document.getElementById("ontology_url")


logger = logging.getLogger("xul_ontologies")


############################################################

def onLoad():
	
	try:
		watchedOnLoad()
	except Exception, e:
		PROMPTS.alert(window, "Error", "Problem accessing the needed elements to use Nepomuk Foxtrot. Please check that nepomukserver is up and running (Error is \"%s\")." % e)
		window.close()

############################################################

def watchedOnLoad():
	
	logger.debug("onLoad()")
	
	document.page_uri = window.opener.content.document.location.href
	
	# attach TheNepomukWhisperer to the document
	from the_nepomuk_whisperer import TheNepomukWhisperer
	document.NEPO = TheNepomukWhisperer(ONTOLOGIES_STORAGE_FILE)
	
	# retrieve the data possibly stored in PATH_CONTENT/ontologies.pkl
	
	ontos_yes = document.NEPO.ontos_mgr.getSelectedOntologies()
	
	# use the retrieved (or not) set along with a request on the database
	ontologies = document.NEPO.ontos_mgr.getAllOntologies()
	
	# add the ontologies registered in ontologies.pkl.
	for ontology in ontos_yes:
		if ontology in ontologies:
			name = document.NEPO.interface.getImage(ontology, NAO_HASDEFAULTNAMESPACEABBREVIATION)
			if name == None:
				name = document.NEPO.pimo.getName(ontology)
			ONTOLOGIES_YES.appendItem(name, ontology)
			ontologies.remove(ontology)
	
	# add other ontologies: the "not selected" ones and the new ones from the database.
	for ontology in ontologies:
		
		name = document.NEPO.interface.getImage(ontology, NAO_HASDEFAULTNAMESPACEABBREVIATION)
		if name == None:
			name = document.NEPO.pimo.getName(ontology)
		
		if ontology in ontos_yes == 1:
			ONTOLOGIES_YES.appendItem(name, ontology)
		else:
			ONTOLOGIES_NO.appendItem(name, ontology)
	
	ONTOLOGIES_NO.selectedIndex = 0

############################################################

def finish():
	
	logger.debug("finish()")
	
	ontos_yes = set()
	
	for i in range(ONTOLOGIES_YES.childNodes.length):
		
		ontology = ONTOLOGIES_YES.childNodes.item(i).getAttribute("value")
		# register this ontology in the external file for persistence
		ontos_yes.add(ontology)
		# actually load the selected ontologies in database
		document.NEPO.ontos_mgr.importOntology(ontology)
	
	f = file(ONTOLOGIES_STORAGE_FILE, "w")
	pickle.dump(ontos_yes, f)
	f.close()
	
	window.close()

############################################################

def ontologiesYesClick():
	
	if ONTOLOGIES_YES.selectedIndex != -1:
		logger.debug(ONTOLOGIES_YES.selectedItem.getAttribute("label") + " <--> " + ONTOLOGIES_YES.selectedItem.getAttribute("value"))
		LBL_SELECTED_ONTO.setAttribute("value", ONTOLOGIES_YES.selectedItem.getAttribute("value"))

def ontologiesYesDblClick():
	
	if ONTOLOGIES_YES.selectedIndex != -1:
		logger.debug(ONTOLOGIES_YES.selectedItem.getAttribute("label") + " <--> " + ONTOLOGIES_YES.selectedItem.getAttribute("value"))
		LBL_SELECTED_ONTO.setAttribute("value", ONTOLOGIES_YES.selectedItem.getAttribute("value"))
		window.openDialog("chrome://nepomuk/content/ontoTree.xul","dlg","height=600,width=560",ONTOLOGIES_YES.selectedItem.getAttribute("value"))

############################################################

def ontologiesNoClick():
	
	if ONTOLOGIES_NO.selectedIndex != -1:
		logger.debug(ONTOLOGIES_NO.selectedItem.getAttribute("label") + " <--> " + ONTOLOGIES_NO.selectedItem.getAttribute("value"))
		LBL_SELECTED_ONTO.setAttribute("value", ONTOLOGIES_NO.selectedItem.getAttribute("value"))

def ontologiesNoDblClick():
	
	if ONTOLOGIES_NO.selectedIndex != -1:
		logger.debug(ONTOLOGIES_NO.selectedItem.getAttribute("label") + " <--> " + ONTOLOGIES_NO.selectedItem.getAttribute("value"))
		LBL_SELECTED_ONTO.setAttribute("value", ONTOLOGIES_NO.selectedItem.getAttribute("value"))
		window.openDialog("chrome://nepomuk/content/ontoTree.xul","dlg","height=600,width=560",ONTOLOGIES_NO.selectedItem.getAttribute("value"))
############################################################

def add():
	
	if ONTOLOGIES_NO.selectedIndex == -1:
		return 1
	
	# remove element from ONTOLOGIES_NO
	index = ONTOLOGIES_NO.selectedIndex
	elt = ONTOLOGIES_NO.removeChild(ONTOLOGIES_NO.getItemAtIndex(ONTOLOGIES_NO.selectedIndex))
	
	# select new index in ONTOLOGIES_NO
	if index == ONTOLOGIES_NO.childNodes.length:
		ONTOLOGIES_NO.selectedIndex = ONTOLOGIES_NO.childNodes.length-1
	else:
		ONTOLOGIES_NO.selectedIndex = index
	
	# add element into ONTOLOGIES_YES
	if ONTOLOGIES_YES.selectedIndex != -1:
		ONTOLOGIES_YES.insertBefore(elt, ONTOLOGIES_YES.selectedItem)
	else:
		ONTOLOGIES_YES.appendChild(elt)
	
	# select new index in ONTOLOGIES_YES
	ONTOLOGIES_YES.selectedIndex = ONTOLOGIES_YES.getIndexOfItem(elt)
	
	return 0
	
############################################################

def remove():
	
	if ONTOLOGIES_YES.selectedIndex == -1:
		return 1
	
	# remove element from ONTOLOGIES_YES
	index = ONTOLOGIES_YES.selectedIndex
	elt = ONTOLOGIES_YES.removeChild(ONTOLOGIES_YES.getItemAtIndex(ONTOLOGIES_YES.selectedIndex))
	
	# select new index in ONTOLOGIES_YES
	if index == ONTOLOGIES_YES.childNodes.length:
		ONTOLOGIES_YES.selectedIndex = ONTOLOGIES_YES.childNodes.length-1
	else:
		ONTOLOGIES_YES.selectedIndex = index
	
	# add element into ONTOLOGIES_NO
	if ONTOLOGIES_NO.selectedIndex != -1:
		ONTOLOGIES_NO.insertBefore(elt, ONTOLOGIES_NO.selectedItem)
	else:
		ONTOLOGIES_NO.appendChild(elt)
	
	# select new index in ONTOLOGIES_NO
	ONTOLOGIES_NO.selectedIndex = ONTOLOGIES_NO.getIndexOfItem(elt)
	
	return 0

############################################################

def up():
	
	index = ONTOLOGIES_YES.selectedIndex
	if (index == 0) or (index == -1):
		return 1
	
	elt1 = ONTOLOGIES_YES.getItemAtIndex(index - 1)
	elt2 = ONTOLOGIES_YES.getItemAtIndex(index)
	
	ONTOLOGIES_YES.removeChild(elt2)
	ONTOLOGIES_YES.insertBefore(elt2, elt1)
	
	ONTOLOGIES_YES.selectedIndex = index - 1
	
	return 0

############################################################

def down():
	
	index = ONTOLOGIES_YES.selectedIndex
	if (index == ONTOLOGIES_YES.childNodes.length - 1) or (index == -1):
		return 1
	
	elt1 = ONTOLOGIES_YES.getItemAtIndex(index)
	elt2 = ONTOLOGIES_YES.getItemAtIndex(index + 1)
	
	ONTOLOGIES_YES.removeChild(elt2)
	ONTOLOGIES_YES.insertBefore(elt2, elt1)
	
	ONTOLOGIES_YES.selectedIndex = index + 1
	
	return 0

############################################################

def loadOnto():
	
	onto_url = ONTO_URL.value
	ONTO_URL.value = ""
	logger.debug("onto_url = %s" % onto_url)
	
	document.NEPO.ontos_mgr.importOntology(onto_url)

############################################################

def refreshOntos():
	
	# clear both lists
	while ONTOLOGIES_YES.childNodes.length != 0:
		ONTOLOGIES_YES.removeChild(ONTOLOGIES_YES.firstChild)
	
	while ONTOLOGIES_NO.childNodes.length != 0:
		ONTOLOGIES_NO.removeChild(ONTOLOGIES_NO.firstChild)
	
	# reload ontologies
	onLoad()















